import { fetchUserCenter } from '../../services/usercenter/fetchUsercenter';
import { resigerUser } from '../../services/usercenter/fetchPerson';
import { getRight, getUserName } from '../../userData/userData'

import Toast from 'tdesign-miniprogram/toast/index';

const menuData = [
  [
    {
      title: '收货地址',
      tit: '',
      url: '',
      type: 'address',
      icon: 'location',
    },
    {
      title: '子女列表',
      tit: '',
      url: '',
      type: 'students',
      icon: 'usergroup',
    },
    {
      title: '切换学校',
      tit: '',
      url: '',
      type: 'schools',
      icon: 'server',
    },
    {
      title: '客服热线',
      tit: '',
      url: '',
      type: 'service',
      icon: 'service',
    },
    {
      title: '我的二维码',
      tit: '',
      url: '',
      type: 'service',
      icon: 'qrcode',
    },
    {
      title: '公告信息',
      tit: '',
      url: '',
      type: 'service',
      icon: 'flag',
    },
    {
      title: '关于我们',
      tit: '',
      url: '',
      type: 'service',
      icon: 'circle',
    },
    {
      title: '意见反馈',
      tit: '',
      url: '',
      type: 'service',
      icon: 'view-module',
    }
    // {
    //   title: '优惠券',
    //   tit: '',
    //   url: '',
    //   type: 'coupon',
    // },
    // {
    //   title: '积分',
    //   tit: '',
    //   url: '',
    //   type: 'point',
    // },

  ],
];

const orderTagInfos = [
  {
    title: '待付款',
    iconName: 'wallet',
    orderNum: 0,
    tabType: 5,
    status: 1,
  },
  {
    title: '待发货',
    iconName: 'deliver',
    orderNum: 0,
    tabType: 10,
    status: 1,
  },
  {
    title: '待收货',
    iconName: 'package',
    orderNum: 0,
    tabType: 40,
    status: 1,
  },
  {
    title: '退款/售后',
    iconName: 'exchang',
    orderNum: 0,
    tabType: 50,
    status: 1,
  },
];

const getDefaultData = () => ({
  showMakePhone: false,
  userInfo: {
    avatarUrl: '',
    nickName: '正在登录...',
    phoneNumber: '',
  },
  menuData,
  orderTagInfos,
  customerServiceInfo: {},
  currAuthStep: 1,
  showKefu: true,
  versionNo: '',
});

Page({
  data: getDefaultData(),

  onLoad() {
    this.getVersionInfo();
  },

  onShow() {
    this.getTabBar().init();
    this.init();
  },
  onPullDownRefresh() {
    this.init();
  },

  init() {
    this.fetUseriInfoHandle();
  },

  fetUseriInfoHandle() {
    fetchUserCenter().then(
      ({
        userInfo,
        countsData,
        orderTagInfos: orderInfo,
        customerServiceInfo,
      }) => {
        // eslint-disable-next-line no-unused-expressions
        menuData?.[0].forEach((v) => {
          countsData.forEach((counts) => {
            if (counts.type === v.type) {
              // eslint-disable-next-line no-param-reassign
              v.tit = counts.num;
            }
          });
        });
        const info = orderTagInfos.map((v, index) => ({
          ...v,
          ...orderInfo[index],
        }));
        this.setData({
          userInfo: { nickName: getUserName() },
          menuData,
          orderTagInfos: info,
          customerServiceInfo,
          currAuthStep: 2,
        });
        wx.stopPullDownRefresh();
      },
    );
  },

  onClickCell({ currentTarget }) {
    const { type } = currentTarget.dataset;

    switch (type) {
      case 'address': {
        wx.navigateTo({ url: '/pages/usercenter/address/list/index' });
        // Toast({
        //   context: this,
        //   selector: '#t-toast',
        //   message: '暂未开通',
        //   icon: '',
        //   duration: 1000,
        // });
        // break;
      }
      case 'service': {
        // this.openMakePhone();
        wx.navigateTo({ url: '/pages/customer/index' });
        // Toast({
        //   context: this,
        //   selector: '#t-toast',
        //   message: '暂未开通',
        //   icon: '',
        //   duration: 1000,
        // });
        break;
      }
      // case 'help-center': {
      //   Toast({
      //     context: this,
      //     selector: '#t-toast',
      //     message: '你点击了帮助中心',
      //     icon: '',
      //     duration: 1000,
      //   });
      //   break;  
      // }
      // case 'point': {
      //   Toast({
      //     context: this,
      //     selector: '#t-toast',
      //     message: '你点击了积分菜单',
      //     icon: '',
      //     duration: 1000,
      //   });
      //   break;
      // }
      // case 'coupon': {
      //   wx.navigateTo({ url: '/pages/coupon/coupon-list/index' });
      //   break;
      // }
      case 'students': {
        if (getRight() == "tourist") {
          Toast({
            context: this,
            selector: '#t-toast',
            message: '游客不能添加子女',
            icon: '',
            duration: 1000,
          });
          return
        }
        wx.navigateTo({ url: '/pages/usercenter/students/list/index' });
        break;
      }
      case 'schools': {
        if (getRight() == "tourist") {
          Toast({
            context: this,
            selector: '#t-toast',
            message: '游客不能切换学校',
            icon: '',
            duration: 1000,
          });
          return
        }
        wx.navigateTo({ url: '/pages/usercenter/school/index' });
        // Toast({
        //   context: this,
        //   selector: '#t-toast',
        //   message: '暂未开通',
        //   icon: '',
        //   duration: 1000,
        // });
        break;
      }
      default: {
        Toast({
          context: this,
          selector: '#t-toast',
          message: '未知跳转',
          icon: '',
          duration: 1000,
        });
        break;
      }
    }
  },

  jumpNav(e) {
    const status = e.detail.tabType;
    console.log(status)
    if (status === 0) {
      wx.navigateTo({ url: '/pages/order/after-service-list/index' });
    } else {
      const app = getApp();
      app.globalData.orderCenterType = status;
      wx.switchTab({ url: '/pages/order/order-list/index' });
      // wx.navigateTo({ url: `/pages/order/order-list/index?status=${status}` });
    }
  },

  jumpAllOrder() {
    const app = getApp();
    app.globalData.orderCenterType = -1;
    // wx.navigateTo({ url: '/pages/order/order-list/index' });
    wx.switchTab({ url: '/pages/order/order-list/index' });
    // wx.navigateTo({ url: '/pages/usercenter/person-info/index' });
  },

  openMakePhone() {
    this.setData({ showMakePhone: true });
  },

  closeMakePhone() {
    this.setData({ showMakePhone: false });
  },

  call() {
    // wx.navigateTo({ url: '/pages/customer/index' });
    // wx.makePhoneCall({
    //   phoneNumber: this.data.customerServiceInfo.servicePhone,
    // });
  },

  gotoUserEditPage() {
    const { currAuthStep } = this.data;
    if (currAuthStep === 2) {
      wx.navigateTo({ url: '/pages/usercenter/person-info/index' });
    } else {
      this.fetUseriInfoHandle();
    }
  },

  getVersionInfo() {
    const versionInfo = wx.getAccountInfoSync();
    const { version, envVersion = __wxConfig } = versionInfo.miniProgram;
    this.setData({
      versionNo: envVersion === 'release' ? version : envVersion,
    });
  },
});
